package com.jly.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Liwenhao
 * @date 2022/3/18 9:41
 * @description
 */
@Aspect
@Component
@Slf4j
public class LogMonitorAspect {

    private final long MAX_TIME = 5000L;

    @Pointcut("@annotation(com.jly.anno.LogMonitor)")
    private void pointcut(){}


    @Around("pointcut()")
    public void doAround(ProceedingJoinPoint call) throws Throwable {
        //获取
        MethodSignature signature = (MethodSignature) call.getSignature();
        Method method = signature.getMethod();

        Parameter[] parameters = method.getParameters();
        List<String> collect = Arrays.stream(parameters).map(m -> m.toString()).collect(Collectors.toList());
        log.info("[方法]" + method.getName() + "入参" + collect.toString());
        long start = System.currentTimeMillis();
        Object proceed = call.proceed();
        log.info("[方法]" + method.getName() + "出参" + proceed.toString());
        long end = System.currentTimeMillis();
        long res = end - start;
        if (res > MAX_TIME) {
            log.warn("[方法]" + method.getName() + " 耗时：" + res + "毫秒");
        }

    }

}
